package com.hjl.servlet;

import com.hjl.dao.UserDao;
import com.hjl.dao.impl.UserDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author ServivE
 * @version 0.1
 * @className ResgisterServlet
 * @description
 * @date 2024/7/24 16:48
 * @since jdk11
 */
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取页面的username和password
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        UserDao userDao = new UserDaoImpl();
        boolean b = userDao.userRegister(username, password);
        if (b) {
            //设置编码格式
            response.setContentType("text/html;charset=UTF-8");
            //创建out对象 对网页响应输出
            PrintWriter pw = response.getWriter();
            //注册成功跳转登录界面
            pw.println("<script>alert('注册成功');location.href='login.html'</script>");
        }else {
            //注册失败则还在此页面
            response.sendRedirect("register.html");
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
